package com.persondemo.videoappliction.ui.video.presenter;

import android.util.Log;

import com.persondemo.videoappliction.bean.MainBean;
import com.persondemo.videoappliction.bean.UpdataBean;
import com.persondemo.videoappliction.bean.UseInfoBean;
import com.persondemo.videoappliction.net.BaseApi;
import com.persondemo.videoappliction.net.BaseObserver;
import com.persondemo.videoappliction.net.RxSchedulers;
import com.persondemo.videoappliction.ui.base.BasePresenter;
import com.persondemo.videoappliction.ui.video.contract.MainContract;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/3/20.
 */

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {
    BaseApi baseApi;

    @Inject
    public MainPresenter(BaseApi baseApi) {
        this.baseApi = baseApi;
    }

    @Override
    public void getdetilsdatas(String imei) {
        baseApi.getmaindatas(imei)
                .compose(RxSchedulers.<MainBean>applySchedulers())
                .compose(mView.<MainBean>bindToLife())
                .subscribe(new Observer<MainBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull MainBean mainBeen) {
                        if (mainBeen == null) {
                            mView.showFaild();
                            return;
                        } else {
                            mView.loaddata(mainBeen);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("lidaming", e.toString());
                        mView.showFaild();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getupdata() {
        baseApi.updata().compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(new BaseObserver<UpdataBean>() {
                    @Override
                    public void onSuccess(UpdataBean updataBean) {
                        mView.loadupdata(updataBean);
                    }

                    @Override
                    public void onFail(Throwable e) {
                        mView.showFaild();
                    }
                });
    }


}
